package com.yswl.yeeduo.ui.activity;

import java.util.Map;
import java.util.TreeMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.App;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.http.KAsyncTask.TaskDelegate;
import com.yswl.yeeduo.tool.KLog;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.bean.ActiviteDetailBean;
import com.yswl.yeeduo.ui.bean.ActiviteDetailBean2;
import com.yswl.yeeduo.ui.task.ActiveDetailInfoTask;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.ToastUtil;
import com.yswl.yeeduo.view.dialog.SelectSharePopupDialog;

/**
 * 作为极光推送消息的处理 点击通知 跳到本activity
 * 
 * 
 * @author Administrator
 * 
 */
public class JpushWebViewActivity extends BaseActivity implements TaskDelegate {

	protected static final String TAG = "WebView";

	private String mActiveId = "";
	private ActiviteDetailBean detaiBean;
	protected WebView webView;
	public RelativeLayout mLayout;// webView的父控件 为了使web Desroy后移除
	private WebViewClient mWebViewClient;

	private final static String ACTIVEID = "activeId" ;
	public static void actionStart(Context cont,String activeId){
		Intent intent = new Intent(cont, JpushWebViewActivity.class);
		intent.putExtra(ACTIVEID, activeId);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		cont.startActivity(intent);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);
		getData();
		initView();

	}

	private void getData() {
		mActiveId = getIntent().getStringExtra(ACTIVEID);
		sendActiveDetaiRequest();
	}

	private void sendActiveDetaiRequest() {
		ActiveDetailInfoTask task = new ActiveDetailInfoTask(this, -9999, this);
		Map<String, String> params = new TreeMap<String, String>();
		if (KStringTool.isEmpty(mActiveId)) {
			return;
		}
		params.put("activeId", mActiveId);
		String userId = ServiceAuthManager.getUserId(this);
		if (KStringTool.isEmpty(userId)) {
			userId = "-1";
		}
		params.put("userId", userId);
		params.put("api_key", ServiceAuthManager.getToken(this));
		task.execute(params);
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		ActiviteDetailBean2 bean2 = (ActiviteDetailBean2) object;
		ActiviteDetailBean detaiBean = ActiviteDetailBean2.beToBean(bean2);
		this.mTitle.setText(detaiBean.title);
		if (URLUtil.isNetworkUrl(bean2.url) == true) {
			this.webView.loadUrl(bean2.url);
		}
	}

	public void initView() {
		initNav();
		initUI();
	}

	private void initUI() {
		this.webView = (WebView) super.findViewById(R.id.web_view);
		this.mLayout = (RelativeLayout) webView.getParent();
		this.webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);// 指定滚动条样式
		this.webView.getSettings().setSupportZoom(true);// 设置允许缩放
		this.webView.getSettings().setJavaScriptEnabled(true);// 设置使用够执行JS脚本
		this.webView.getSettings().setBlockNetworkImage(true);// 图片下载阻塞
		mWebViewClient = new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				webView.getSettings().setBlockNetworkImage(false); // 使用图片加载
			}

			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				ToastUtil.showShortMsg("网页加载出错！");
			};

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				KLog.e(TAG, "---------网页链接网址-------->>" + url);
				return true;
			};
		};
		this.webView.setWebViewClient(mWebViewClient);

		final LinearLayout showTag = (LinearLayout) super.findViewById(R.id.ll_shareTag);
		showTag.setVisibility(View.VISIBLE);
		LinearLayout closeTag = (LinearLayout) super.findViewById(R.id.ll_shareTagCloseBtn);
		closeTag.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (showTag.getVisibility() == View.VISIBLE) {
					showTag.setVisibility(View.GONE);
				}
			}
		});

		ImageView shareIv = (ImageView) super.findViewById(R.id.iv_share_btn);
		shareIv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 此处分享
				if (App.getInstance().isLogin == false) {
					startActivity(new Intent(JpushWebViewActivity.this, LoginActivity.class));
					return;
				}
				if (detaiBean != null) {
					SelectSharePopupDialog dialog = new SelectSharePopupDialog(JpushWebViewActivity.this);
					dialog.setActiveBean(detaiBean);
					dialog.showAtLocation(mLayout, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
				}
			}
		});

	}

	private void initNav() {
		super.mTitle = (TextView) super.findViewById(R.id.nav_title);
		super.mBack = (ImageView) super.findViewById(R.id.nav_left);
		mBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				startActivity(new Intent(JpushWebViewActivity.this, MainActivity.class));
//				overridePendingTransition(0, 0);
				finish();
			}
		});

	}

	protected void onPause() {
		super.onPause();
		this.webView.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);
	};
	@Override
	protected void onResume() {
		super.onResume();
		this.webView.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this); // 统计时长
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
			webView.goBack(); // goBack()表示返回WebView的上一页面
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.finish();
			return true;
		}
		return false;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 以下代码防止 webView 报错
		this.mLayout.removeView(webView);
		webView.removeAllViews();
		webView.destroy();
	}

	
	
}
